package ${basePackageName}.interfaces.assembler;

<#if common>
import com.zy.explore.common.infrastructure.util.DataBeanUtil;
import com.zy.explore.common.interfaces.frm.CommonIdFrm;
import com.zy.explore.common.interfaces.frm.CommonIdsFrm;
import com.zy.explore.common.interfaces.vo.PageVO;
<#else>
import ${basePackageName}.infrastructure.util.DataBeanUtil;
import ${basePackageName}.interfaces.frm.CommonIdFrm;
import ${basePackageName}.interfaces.frm.CommonIdsFrm;
import ${basePackageName}.interfaces.vo.PageVO;
</#if>
import ${basePackageName}.interfaces.validator.<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}Validator;
import ${basePackageName}.domain.jpa.entity.<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix};
import ${basePackageName}.interfaces.frm.${tableClassNamePrefix}SaveFrm;
import ${basePackageName}.interfaces.frm.${tableClassNamePrefix}QueryFrm;
import ${basePackageName}.interfaces.vo.${tableClassNamePrefix}VO;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
<#if pkType != 'String'>
import java.util.stream.Collectors;
</#if>

/**
 * description 表(${tableName})对应的JpaAssembler
 *
 * @author ${authorName}
 * @date ${dateTime}
 */
@Component
public class <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}Assembler {

    /**
     * 新增数据
     *
     * @param frm 待新增的frm对象
     * @return 成功返回新增后的VO对象，失败返回null
     */
    public ${tableClassNamePrefix}VO insert(${tableClassNamePrefix}SaveFrm frm) {
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = convertFromFrm(frm);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return convertToVO(entity.save());
    }

    /**
     * 删除数据
     *
     * @param frm 主键frm对象
     * @return 1:删除成功 其他:删除失败
     */
    public Integer delete(CommonIdFrm frm) {
    <#if pkType == 'Integer'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Integer.parseInt(frm.get${pkPropNameFirstUpper}())).build();
    <#elseif pkType == 'Long'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Long.parseLong(frm.get${pkPropNameFirstUpper}())).build();
    <#elseif pkType == 'String'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(frm.get${pkPropNameFirstUpper}()).build();
    </#if>
        entity.remove();
        return 1;
    }

    /**
     * 更新数据
     *
     * @param frm 待更新的frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public ${tableClassNamePrefix}VO update(${tableClassNamePrefix}SaveFrm frm) {
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = convertFromFrm(frm);
        entity.setUpdateTime(new Date());
        return convertToVO(entity.updateSelective());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public ${tableClassNamePrefix}VO findById(CommonIdFrm frm) {
    <#if pkType == 'Integer'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Integer.parseInt(frm.get${pkPropNameFirstUpper}())).build();
    <#elseif pkType == 'Long'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(Long.parseLong(frm.get${pkPropNameFirstUpper}())).build();
    <#elseif pkType == 'String'>
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().${pkPropName}(frm.get${pkPropNameFirstUpper}()).build();
    </#if>
        return convertToVO(entity.findById());
    }

    /**
    * 通过主键批量查询数据集合
    *
    * @param frm 主键集合Frm对象
    * @return 成功返回对应ID的VO对象，失败返回空集合
    */
    public List<${tableClassNamePrefix}VO> findByIds(CommonIdsFrm frm) {
        List<${tableClassNamePrefix}VO> retList = new ArrayList<>();
            <#if pkType == 'Integer'>
        List<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> list = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds().stream().map(Integer::parseInt).collect(Collectors.toList()));
            <#elseif pkType == 'Long'>
        List<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> list = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds().stream().map(Long::parseLong).collect(Collectors.toList()));
            <#elseif pkType == 'String'>
        List<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> list = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().build().findByIds(frm.getIds());
            </#if>
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
        }
        return retList;
    }

    /**
     * 根据条件获取数据集
     *
     * @param frm 查询条件对象
     * @return 成功返回满足条件的数据集合，失败返回空集合
     */
    public List<${tableClassNamePrefix}VO> findList(${tableClassNamePrefix}QueryFrm frm) {
        List<${tableClassNamePrefix}VO> retList = new ArrayList<>();
        // 转换查询条件
        Specification<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> specification = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.getSpecification(frm);
        // 转换排序条件
        Sort sort = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.getSort(frm.getSortFields());
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().build();
        List<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> list = entity.findAll(specification, sort);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
        }
        return retList;
    }

    /**
     * 根据条件查询数据分页
     *
     * @param frm 查询条件参数
     * @return 成功返回满足条件的分页，失败返回空页
     */
    public PageVO<${tableClassNamePrefix}VO> findPage(${tableClassNamePrefix}QueryFrm frm) {
        // 转换查询条件
        Specification<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> specification = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.getSpecification(frm);
        // 转换排序条件
        Sort sort = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.getSort(frm.getSortFields());
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.builder().build();
        Page<<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}> page = entity.findAll(specification, frm.getPageNum(), frm.getPageSize(), sort);
        List<${tableClassNamePrefix}VO> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getContent())) {
            page.getContent().forEach(x -> voList.add(convertToVO(x)));
        }
        return new PageVO<>(new PageImpl<>(voList, page.getPageable(), page.getTotalElements()));
    }

    /**
     * 实体类转VO
     *
     * @param entity 实体类
     * @return 转换后的对象
     */
    public ${tableClassNamePrefix}VO convertToVO(<#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity) {
        if (entity == null) {
            return null;
        }
        ${tableClassNamePrefix}VO vo = DataBeanUtil.copyOf(entity, ${tableClassNamePrefix}VO.class, true);
        <#if pkType != 'String'>
        vo.setId(entity.getId().toString());
        <#else>
        vo.setId(entity.getId());
        </#if>
        return vo;
    }

    /**
     * Frm转实体类
     *
     * @param frm 参数对象
     * @return 转换后的实体类对象
     */
    public <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} convertFromFrm(${tableClassNamePrefix}SaveFrm frm) {
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}Validator.validateFrm(this, frm);
        <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix} entity = DataBeanUtil.copyOf(frm, <#if prefixJpa??>${prefixJpa}</#if>${tableClassNamePrefix}.class, true);
        if (!StringUtils.isEmpty(frm.getId())) {
        <#if pkType == 'Integer'>
            entity.setId(Integer.parseInt(frm.getId()));
        <#elseif pkType == 'Long'>
            entity.setId(Long.parseLong(frm.getId()));
        <#elseif pkType == 'String'>
            entity.setId(frm.getId());
        </#if>
        }
        return entity;
    }

}
